//
//  DCIMBaseTabBarViewController.m
//  xmppBase
//
//  Created by striby on 16/1/7.
//  Copyright © 2016年 com.dc. All rights reserved.
//

#import "DCIMBaseTabBarViewController.h"
#import "DCIMChatListViewController.h"
#import "DCIMConversationViewController.h"
#import "DCIMSettingViewController.h"
#import "DCIMFriendListViewController.h"
#import "KxMenu.h"
#import "DCIMAddFriendViewController.h"
@interface DCIMBaseTabBarViewController ()
{
    BOOL menuShowed;
}
@end

@implementation DCIMBaseTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.delegate = self;
    
    DCIMChatListViewController * chatList = [[DCIMChatListViewController alloc]init];
    
    DCIMFriendListViewController * friendlist = [[DCIMFriendListViewController alloc]init];
    friendlist.friendListType = DCFriendListTypeNormal;
    
    DCIMSettingViewController * setting = [[DCIMSettingViewController alloc]init];
    
    [self setViewControllers:  @[chatList,friendlist,setting]];
    
    NSArray * items =  self.tabBar.items;
    
    UITabBarItem * itemChatList = [items firstObject];
    
    itemChatList.tag = 0;
    
    itemChatList.title = @"消息";
    
    itemChatList.selectedImage = [UIImage imageNamed:@"icon_chat_hover"];
    
    itemChatList.image = [UIImage imageNamed:@"icon_chat"];
    
    UITabBarItem * itemFriendList = items[1];

    itemFriendList.tag=1;
    
    itemFriendList.title = @"好友";
    
    itemFriendList.selectedImage = [UIImage imageNamed:@"icon_group_hover"];
    
    itemFriendList.image = [UIImage imageNamed:@"icon_group"];
    
    UITabBarItem * itemSetting = items[2];
    
    itemSetting.tag=2;
    
    itemSetting.title = @"我";
    
    itemSetting.selectedImage = [UIImage imageNamed:@"icon_me_hover"];
    
    itemSetting.image = [UIImage imageNamed:@"icon_me"];
    
    [self addRightItem];
    

}


#pragma mark - delegate
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item.tag==0) {
        //自定义rightBarButtonItem
        [self addRightItem];
        
    }
    else
    {
        self.navigationItem.rightBarButtonItems=@[];
    }
}

-(void)addRightItem
{
  
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showMenu:) ];
   
    self.navigationItem.rightBarButtonItem = rightButton;
}

/**
 *  弹出层
 *
 *  @param sender sender description
 */
- (IBAction)showMenu:(UIButton *)sender {
    
    if (menuShowed) {
        [KxMenu dismissMenu];
        menuShowed = NO;
        return;
    }
    
    NSArray *menuItems =
    @[
      
      [KxMenuItem menuItem:@"发起聊天"
                     image:[UIImage imageNamed:@"chat_icon"]
                    target:self
                    action:@selector(pushChat:)],
      
      [KxMenuItem menuItem:@"添加好友"
                     image:[UIImage imageNamed:@"addfriend_icon"]
                    target:self
                    action:@selector(pushAddFriend:)],

      
      [KxMenuItem menuItem:@"发起群聊"
                     image:[UIImage imageNamed:@"public_account"]
                    target:self
                    action:@selector(pushDiscuss:)],

      ];
    
    CGRect targetFrame = CGRectMake(DCDeviceWidth-35, 20, 10, 10);
    targetFrame.origin.y = targetFrame.origin.y + 15;
    [KxMenu showMenuInView:self.viewControllers[0].view
                  fromRect:targetFrame
                 menuItems:menuItems];
    menuShowed=YES;
}

#pragma mark - method
-(void)pushChat:(id)sender
{
    menuShowed = NO;
    
    DCIMFriendListViewController * vc =[[DCIMFriendListViewController alloc]init];
    vc.friendListType =DCFriendListTypePrivateChat;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)pushAddFriend:(id)sender
{
    menuShowed = NO;
    
    DCIMAddFriendViewController * vc =[[DCIMAddFriendViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)pushDiscuss:(id)sender
{
    menuShowed = NO;
    
    DCIMFriendListViewController * vc =[[DCIMFriendListViewController alloc]init];
    vc.friendListType =DCFriendListTypeDiscussion;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)setUnReadCout:(int)unReadCount
{
    NSArray * items =  self.tabBar.items;
    
    UITabBarItem * item = [items firstObject];
    
    if (unReadCount!=0) {
        [item setBadgeValue:[NSString stringWithFormat:@"%D",unReadCount]];

    }
    else
    {
        [item setBadgeValue:nil];
    }
    
    
    [UIApplication sharedApplication].applicationIconBadgeNumber = unReadCount;
 }

@end
